<template>
  <div class="operation-page">
    <h2>节点组管理</h2>
    <div class="button-group">
      <el-button type="primary" @click="openAddNodeGroupDialog"
        >新增节点组</el-button
      >
      <el-button @click="goBack">返回</el-button>
    </div>
    <el-table :data="nodeGroups" style="width: 100%">
      <el-table-column prop="name" label="节点组名称" />
      <el-table-column label="负载">
        <template #default="{ row }"> {{ row.load }}% </template>
      </el-table-column>
      <el-table-column label="操作">
        <template #default="{ row }">
          <el-button @click="editNodeGroup(row)">编辑</el-button>
          <el-button @click="deleteNodeGroup(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="节点组" v-model="nodeGroupDialogVisible" width="30%">
      <el-form>
        <el-form-item label="节点组名称">
          <el-input
            v-model="currentNodeGroupName"
            placeholder="请输入节点组名称"
          />
        </el-form-item>
      </el-form>
      <template v-slot:footer>
        <el-button @click="nodeGroupDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="saveNodeGroup">保存</el-button>
      </template>
    </el-dialog>

    <el-dialog title="预警邮箱设置" v-model="emailDialogVisible" width="30%">
      <el-form>
        <el-form-item label="邮箱地址">
          <el-input v-model="emailAddress" placeholder="请输入邮箱地址" />
        </el-form-item>
      </el-form>
      <template v-slot:footer>
        <el-button @click="emailDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="saveEmailSettings">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref } from "vue";
import { useRouter } from "vue-router";

const router = useRouter();

const nodeGroups = ref([
  { name: "组1", load: 20 },
  { name: "组2", load: 50 },
  { name: "组3", load: 80 },
]);

const nodeGroupDialogVisible = ref(false);
const emailDialogVisible = ref(false);
const currentNodeGroupName = ref("");
const emailAddress = ref("");
const isEditMode = ref(false);

const openAddNodeGroupDialog = () => {
  isEditMode.value = false;
  currentNodeGroupName.value = "";
  nodeGroupDialogVisible.value = true;
};

const editNodeGroup = (group) => {
  isEditMode.value = true;
  currentNodeGroupName.value = group.name;
  nodeGroupDialogVisible.value = true;
};

const saveNodeGroup = () => {
  if (isEditMode.value) {
    // 编辑逻辑
    console.log("编辑节点组:", currentNodeGroupName.value);
  } else {
    // 新增逻辑
    console.log("新增节点组:", currentNodeGroupName.value);
    nodeGroups.value.push({ name: currentNodeGroupName.value, load: 0 });
  }
  nodeGroupDialogVisible.value = false;
};

const saveEmailSettings = () => {
  // 保存邮箱设置逻辑
  console.log("保存邮箱地址:", emailAddress.value);
  emailDialogVisible.value = false;
};

const deleteNodeGroup = (group) => {
  // 删除逻辑
  console.log("删除节点组:", group.name);
  nodeGroups.value = nodeGroups.value.filter((g) => g.name !== group.name);
};

const goBack = () => {
  router.back();
};
</script>

<style scoped>
.operation-page {
  padding: 20px;
}

.button-group {
  margin-bottom: 20px;
}

.el-table {
  margin-top: 20px;
}
</style> 